/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HUDTEXTBOX_H
#define HUDTEXTBOX_H
#include "TimerModule.h"
#include "HUDModule.h"
#include "HUDElement.h"
#include "HUDScrollbar.h"
#include "OpenglModule.h"


#include <string>

// Textbox HUD element

namespace ModularEngine
{
	class CLASSDECL HUDTextbox: public HUDElement
	{
	protected:

		// Timer to control cursor flicker
		ModularEngine::Timer mCursorTimer;

		//char *mpText;  // text in the textbox;
	public:
		std::string msText;

	protected:

		// Cursor position
		int mCursor;

		// textoffset based off scrolling
		float mxScroll;

		// variable width of a character.
		float mCharWidth;

		// Scrollbar for multiline textbox.
		HUDScrollbar *mScrollbar;

		Vector2d mvOriginalPosition;

		// 
		int mLines;

		// Textbox Memory
		int nRecallNumber; 
		std::vector<std::string> msTextRecall;
	
	public:
		bool bMemory;

		Functor *mtFunc;

		// is multiline?
		bool mbMultiline;

		// accepts input?
		bool mbInput;

		HUDTextbox();	
		~HUDTextbox();

		int initialize();
		void draw();

		void drawSingleline();
		void drawMultiline();

		std::string getText()
		{
			return msText;
		}

		void setText( char *pText )
		{
			msText = pText;
			mCursor = strlen( msText.c_str() );
			adjustScroll();
		}

		void mouseInput( Vector2d vPos, UINT nDown );
		void keyboardInput( UINT nKey );

		// Adjust scroll
		void adjustScroll();

		// Sets msText = "";
		void clearTextBox();

		// Adds a new line in the textbox;
		void addText( const char *text );
		void addText( std::string str )
		{
			addText( str.c_str() );
		}

		// Draws the text on multiple lines.
		void drawMultilineText();

		int findLastSpace( int nWidth, std::string str );

		int countLines( std::string str, int nWidth, int nCharWidth = 9 );


	};
}

#endif
